package com.pengpeng.jjwt.controller;

import com.pengpeng.jjwt.annotation.JwtIgnore;
import com.pengpeng.jjwt.entity.JwtParam;
import com.pengpeng.jjwt.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    JwtParam jwtParam;

    @JwtIgnore
    @RequestMapping(path = "/login", method = {RequestMethod.GET, RequestMethod.POST})
    public String login(String username, String password) {
        if ("mr".equals(username) && "123".equals(password)) {
            // 用户名密码验证通过
            Integer userId = 5;
            // 生成token
            String token = JwtUtils.createToken(userId + "", jwtParam);
            if (token == null) {
                log.error("===== 用户签名失败 =====");
                return null;
            }
            log.info("===== 用户{}生成签名{} =====", userId, token);
            return JwtUtils.getAuthorizationHeader(token);
        }
        log.info("===== 用户名或密码错误 =====");

        return null;
    }

    /**
     * 验证
     * @Author pengp
     * @Date 2022/3/3 16:39
     * @return java.lang.String
     */
    @RequestMapping("/hello")
    public String hello() {
        return "Hello World!";
    }

}
